package com.jshoperxms.controller.mall.backstage.advertise;

import com.jshoperxms.Response;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.mall.backstage.base.IBaseCtrl;
import com.jshoperxms.controller.mall.backstage.base.SearchModel;
import com.jshoperxms.controller.utils.BaseTools;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.AdvisterEnums;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.statickey.SerialKeys;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.controller.utils.statickey.WarnWrods;
import com.jshoperxms.entity.AdvertiseContentRpT;
import com.jshoperxms.entity.AdvertiseT;
import com.jshoperxms.entity.GoodsSpecialT;
import com.jshoperxms.entity.ProductT;
import com.jshoperxms.service.AdvertiseContentRpTService;
import com.jshoperxms.service.GoodsSpecialTService;
import com.jshoperxms.service.ProductTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.*;

/**
 * Created by sdywcd on 16/11/15.
 * Des:
 */
@Controller
@RequestMapping("/mall/advertisecontent")
public class AdvertiseContentRpTController extends BaseTController implements IBaseCtrl<AdvertiseContentRpT> {


    @Resource
    private AdvertiseContentRpTService advertiseContentRpTService;

    @Resource
    private ProductTService productTService;

    @Resource
    private GoodsSpecialTService goodsSpecialTService;

    private Map<String,Object> responsedata=new HashMap<>();

    @RequestMapping(value="/save",method= RequestMethod.POST)
    @Override
    public ResponseEntity save(@RequestBody AdvertiseContentRpT rq) throws ParseException {
        AdvertiseContentRpT bean=new AdvertiseContentRpT();
        bean.setId(this.serial.Serialid(SerialKeys.ADVISTERCONTENTRP));
        bean.setAdvertiseid(rq.getAdvertiseid());
        if(StringUtils.equals(rq.getType(), AdvisterEnums.AdvContentRpType.PRODUCT.getState())){
            StringBuffer stringBuffer=new StringBuffer();
            stringBuffer.append("["+ AdvisterEnums.AdvContentRpType.PRODUCT.getName()+"]");
            ProductT pbean=this.productTService.findByPK(ProductT.class,rq.getRpid());
            if(pbean!=null){
                stringBuffer.append(pbean.getProductname());
                bean.setMainimg(pbean.getMainimg());
                bean.setSmainimg(pbean.getSmainimg());
            }
            bean.setRptitle(stringBuffer.toString());
        }
        if(StringUtils.equals(rq.getType(), AdvisterEnums.AdvContentRpType.GOODSSPECIAL.getState())){
            StringBuffer stringBuffer=new StringBuffer();
            stringBuffer.append("["+ AdvisterEnums.AdvContentRpType.GOODSSPECIAL.getName()+"]");
            GoodsSpecialT gbean=this.goodsSpecialTService.findByPK(GoodsSpecialT.class,rq.getRpid());
            if(gbean!=null){
                stringBuffer.append(gbean.getTitle());
                bean.setMainimg(gbean.getMainimg());
                bean.setSmainimg(gbean.getSmainimg());
            }
            bean.setRptitle(stringBuffer.toString());

        }
        bean.setRpid(rq.getRpid());
        bean.setType(rq.getType());
        bean.setCreatetime(DateTools.getSystemTimeBi());
        bean.setCreatorid(BaseTools.getAdminCreateId());
        bean.setUpdatetime(DateTools.getSystemTimeBi());
        bean.setStatus(rq.getStatus());
        bean.setVersiont(1);
        this.advertiseContentRpTService.save(bean);
        responsedata.put(TagWords.SUCCESS, true);
        responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        return Response.success(responsedata);
    }

    @Override
    public ResponseEntity find(@RequestBody AdvertiseContentRpT rq) {
        return null;
    }


    @RequestMapping(value="/del",method= RequestMethod.POST)
    @Override
    public ResponseEntity del(@RequestBody AdvertiseContentRpT rq) {
        if(StringUtils.isBlank(rq.getId())){
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        }else{
            String ids[]=StringUtils.split(rq.getId(),TagWords.SPLITDOT);
            for(String s:ids){
                AdvertiseContentRpT bean=this.advertiseContentRpTService.findByPK(AdvertiseContentRpT.class,s);
                if(bean!=null){
                    bean.setStatus(BaseEnums.DataUsingState.DEL.getState());
                    bean.setUpdatetime(DateTools.getSystemTimeBi());
                    this.advertiseContentRpTService.update(bean);
                }
            }
            responsedata.put(TagWords.SUCCESS,true);
            responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        }
        return Response.success(responsedata);
    }

    @Override
    public ResponseEntity update(@RequestBody AdvertiseContentRpT rq) throws ParseException {
        return null;
    }

    @Override
    public ResponseEntity findAll() {
        return null;
    }

    @RequestMapping(value="/findByPage",method= RequestMethod.POST)
    @Override
    public ResponseEntity findByPage(SearchModel searchModel, @RequestParam("search[value]") String search, @RequestParam("order[0][column]") String ordervalue, @RequestParam("order[0][dir]") String ordertype) {
        findCustomSearch(searchModel,search,ordervalue,ordertype);
        return Response.success(responsedata);
    }

    @Override
    public void findCustomSearch(SearchModel searchModel, String search, String ordervalue, String ordertype) {
        if(searchModel.getIsCustomSearch()==1){
            int currentPage=searchModel.getCalCurrentPage();
            int lineSize = searchModel.getLength();
            Criterion criterion= Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
            if(StringUtils.isNotBlank(searchModel.getName())){
                criterion=Restrictions.and(criterion).add(Restrictions.like("rptitle",searchModel.getName(), MatchMode.ANYWHERE));
            }
            if(StringUtils.isNotBlank(search)){
                criterion=Restrictions.and(criterion).add(Restrictions.like("rptitle",search,MatchMode.ANYWHERE));
            }
            int records=this.advertiseContentRpTService.count(AdvertiseContentRpT.class,criterion).intValue();
            searchModel.setRecordsTotal(records);
            searchModel.setRecordsFiltered(records);
            responsedata.put(TagWords.RECORDSTOTAL,records);
            responsedata.put(TagWords.RECORDSFILTERED,records);
            List<AdvertiseContentRpT> list = this.advertiseContentRpTService.findByCriteriaByPage(
                    AdvertiseContentRpT.class, criterion, Order.desc(TagWords.UPDATETIME), currentPage,
                    lineSize);
            this.processPageList(list);

        }else{
            if(StringUtils.isNotBlank(search)){
                filterSearch(searchModel,search);
            }else{
                findDefaultAll(searchModel,ordervalue,ordertype);
            }
        }
    }

    @Override
    public void filterSearch(SearchModel searchModel, String search) {
        int currentPage=searchModel.getCalCurrentPage();
        int lineSize = searchModel.getLength();
        Criterion criterion=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
        criterion=Restrictions.and(criterion).add(Restrictions.like("rptitle",search,MatchMode.ANYWHERE));
        int records=this.advertiseContentRpTService.count(AdvertiseContentRpT.class,criterion).intValue();
        searchModel.setRecordsTotal(records);
        searchModel.setRecordsFiltered(records);
        responsedata.put(TagWords.RECORDSTOTAL,records);
        responsedata.put(TagWords.RECORDSFILTERED,records);
        List<AdvertiseContentRpT> list = this.advertiseContentRpTService.findByCriteriaByPage(
                AdvertiseContentRpT.class, criterion,Order.desc(TagWords.UPDATETIME), currentPage,
                lineSize);
        this.processPageList(list);
    }

    @Override
    public void findDefaultAll(SearchModel searchModel, String ordervalue, String ordertype) {
        int currentPage=searchModel.getCalCurrentPage();
        int lineSize = searchModel.getLength();
        Criterion criterion=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
        int records=this.advertiseContentRpTService.count(AdvertiseContentRpT.class,criterion).intValue();
        searchModel.setRecordsFiltered(records);
        searchModel.setRecordsTotal(records);
        responsedata.put(TagWords.RECORDSTOTAL,records);
        responsedata.put(TagWords.RECORDSFILTERED,records);
        Order order= BaseTools.orderByParam("rptitle",ordervalue,ordertype);
        List<AdvertiseContentRpT> list = this.advertiseContentRpTService.findByCriteriaByPage(
                AdvertiseContentRpT.class, criterion,order, currentPage,
                lineSize);
        this.processPageList(list);
    }

    @Override
    public void processPageList(List<AdvertiseContentRpT> list) {
        List<Map<String,Object>>data=new ArrayList<>();
        for(Iterator<AdvertiseContentRpT> it = list.iterator(); it.hasNext();){
            AdvertiseContentRpT bean=it.next();
            Map<String,Object>cellMap=new HashMap<>();
            cellMap.put("id",bean.getId());
            cellMap.put("smainimg",bean.getSmainimg());
            cellMap.put("rptitle",bean.getRptitle());
            cellMap.put("type", AdvisterEnums.AdvContentRpType.getName(bean.getType()));
            cellMap.put("rpid",bean.getRpid());
            cellMap.put("status", BaseEnums.DataUsingState.getName(bean.getStatus()));
            cellMap.put("updatetime", DateTools.formateLongDateToString(bean.getUpdatetime().longValue()));
            cellMap.put("version", bean.getVersiont());
            data.add(cellMap);
        }
        responsedata.put(TagWords.DATA,data);
    }

    @Override
    public boolean checkDuplicates(String str) {
        return false;
    }
}
